#!/bin/bash

# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function prepare_flutter_engine_src() {
    if [ ! -d "flutter_engine_src" ]
    then
        git clone https://gitcode.com/shijie_251314/flutter_engine_src.git
        git -C flutter_engine_src lfs pull
        if [ $? -ne 0 ]
        then
            echo "clone flutter_engine_src failed!"
            rm -rf flutter_engine_src
            return 1
        fi
    fi

    rm -rf src
    cp -r flutter_engine_src/src .

    rm -rf depot_tools
    cp -r flutter_engine_src/depot_tools .

    rm -rf flutter_env
    cp -r flutter_engine_src/flutter_env .

    rm -rf .vpython_cipd_cache
    rm -rf .vpython-root
    cp -r flutter_engine_src/flutter-vpython/.vpython_cipd_cache .
    cp -r flutter_engine_src/flutter-vpython/.vpython-root .

    cp -r third_party/flutter/engine/. src/flutter/
    return 0
}

function prepare_flutter_git_bak() {
    if [ ! -d "flutter_git_bak" ]
    then
        git clone https://gitcode.com/shijie_251314/flutter_git_bak.git
        git -C flutter_git_bak lfs pull
        if [ $? -ne 0 ]
        then
            echo "clone flutter_git_bak failed!"
            rm -rf flutter_git_bak
            return 1
        fi
    fi

    mkdir src/third_party
    tar -xzf flutter_git_bak/dart.tar.gz -C src/third_party/
    tar -xzf flutter_git_bak/libcxx.tar.gz -C src/third_party/
    tar -xzf flutter_git_bak/libcxxabi.tar.gz -C src/third_party/
    tar -xzf flutter_git_bak/zlib.tar.gz -C src/third_party/

    chmod -R 777 src
    return 0
}

function prepare_commandlinetools(){
    echo "prepare_commandlinetools"

    if [ -d "command-line-tools" ];then
        echo "command-line-tools exists, do not download again."
    else
        if [ ! -f "command-line-tools.zip" ];then
            echo "start download command-line-tools.zip"
            curl -o "command-line-tools.zip" https://repo.huaweicloud.com/harmonyos/ohpm/5.0.5/commandline-tools-linux-x64-5.0.5.310.zip

            # 判断是否下载成功
            if [ $? != 0 ];then
                echo "download command-line-tools.zip failed."
                rm -rf command-line-tools.zip
                return 1
            fi
        fi

        # 开始解压zip包
        echo "start unzip command-line-tools.zip"
        unzip -oqq "./command-line-tools.zip"

        echo "finish unzip command-line-tools.zip"
    fi

    return 0
}

function prepare() {
    apt-get update && apt-get install -y python3 unzip git-lfs make ninja-build pkg-config tree tar curl

    prepare_flutter_engine_src
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_engine_src failed!!"
        return 1
    fi

    prepare_flutter_git_bak
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_engine_third_party failed!!"
        return 1
    fi

    prepare_commandlinetools
    if [ $? -ne 0 ]
    then
        echo "prepare_commandlinetools failed!!"
        return 1
    fi
	
    #创建npmrc文件
    rm -f ~/.npmrc
    echo -e "registry=https://repo.huaweicloud.com/repository/npm/\n@ohos:registry=https://repo.harmonyos.com/npm/\nlockfile=false" > ~/.npmrc
    rm -rf ~/.ohpm
    mkdir ~/.ohpm
    touch ~/.ohpm/.ohpmrc
    echo -e "registry=https://ohpm.openharmony.cn/ohpm/" > ~/.ohpm/.ohpmrc
    
    # 删除hvigor缓存
    rm -rf ~/.hvigor

    python3 src/flutter/attachment/scripts/ohos_setup.py && ./setup.sh
    echo "--> Finish prepare.sh ."

    return 0
}

prepare $@

exit $?

#eof
